<template>
  <div class="roles">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-button type="primary" @click="addRolesShow = true">添加角色</el-button>
      <template>
        <el-table :data="rolesList" border stripe>
          <el-table-column type="expand">
            <template slot-scope="scope">
              <el-row
                :class="['dbbottom',i1===0?'dbtop':'','vcenter']"
                v-for="(item1,i1) in scope.row.children"
                :key="i1"
              >
                <el-col :span="5">
                  <el-tag @close="deletePowers(scope.row,item1.id)" closable>{{item1.authName}}</el-tag>
                  <i class="el-icon-caret-right"></i>
                </el-col>
                <el-col :span="19">
                  <el-row
                    :class="[i2===0?'':'dbtop','vcenter']"
                    v-for="(item2,i2) in item1.children"
                    :key="i2"
                  >
                    <el-col :span="6">
                      <el-tag
                        type="success"
                        @close="deletePowers(scope.row,item2.id)"
                        closable
                      >{{item2.authName}}</el-tag>
                      <i class="el-icon-caret-right"></i>
                    </el-col>
                    <el-col :span="18">
                      <el-tag
                        type="warning"
                        v-for="(item3,i3) in item2.children"
                        closable
                        @close="deletePowers(scope.row,item3.id)"
                        :key="i3"
                      >{{item3.authName}}</el-tag>
                    </el-col>
                  </el-row>
                </el-col>
              </el-row>
            </template>
          </el-table-column>
          <el-table-column label="ID" type="index"></el-table-column>
          <el-table-column label="角色名称" prop="roleName"></el-table-column>
          <el-table-column label="角色描述" prop="roleDesc"></el-table-column>

          <el-table-column label="操作" width="300">
            <template slot-scope="scope">
              <el-button
                size="mini"
                icon="el-icon-edit"
                type="primary"
                @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button>
              <el-button
                size="mini"
                icon="el-icon-delet"
                type="danger"
                @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button>
              <el-button
                size="mini"
                icon="el-icon-setting"
                type="warning"
                @click="setPower(scope.row)"
              >角色分配</el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
    </el-card>
    <!-- 添加角色 -->
    <el-dialog title="添加角色" :visible.sync="addRolesShow" width="50%" @close="addDialogClosed">
      <el-form
        :model="addRoles"
        status-icon
        :rules="Rolesrules"
        ref="RolesRef"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="addRoles.roleName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="roleDesc">
          <el-input v-model="addRoles.roleDesc" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addRolesShow = false">取 消</el-button>
        <el-button type="primary" @click="addRolesUser">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 编辑角色 -->
    <el-dialog title="编辑角色" :visible.sync="EditRolesShow" width="50%" @close="EditDialogClosed">
      <el-form
        :model="editRoles"
        status-icon
        :rules="EditRolesrules"
        ref="EditRolesRef"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="editRoles.roleName"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="roleDesc">
          <el-input v-model="editRoles.roleDesc"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editRolesShow = false">取 消</el-button>
        <el-button type="primary" @click="EditRolesUser">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 权限分配 -->
    <el-dialog title="权限分配" :visible.sync="setPowerDialog" width="50%" @close="clearDefKeys">
      <el-tree
        :data="rightsList"
        :props="defaultProps"
        show-checkbox
        node-key="id"
        ref="treeRef"
        default-expand-all
        :default-checked-keys="defKeys"
      ></el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="setPowerDialog = false">取 消</el-button>
        <el-button type="primary" @click="addPower">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: 'Roles',
  data() {
    return {
      rolesList: [],
      addRolesShow: false,
      EditRolesShow: false,
      addRoles: { roleName: '', roleDesc: '' },
      Rolesrules: {
        roleName: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' }
        ]
      },
      EditRolesrules: {
        roleName: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' }
        ]
      },
      editRoles: {
        // roleName: '',
        // roleDesc: ''
      },
      setPowerDialog: false,
      rightsList: [],
      defaultProps: {
        children: 'children',
        label: 'authName'
      },
      defKeys: [],
      roleId: ''
    }
  },
  created() {
    this.getRolesList()
  },
  methods: {
    getRolesList() {
      this.$axios.get('roles').then(result => {
        // console.log(result.data)
        this.rolesList = result.data.data
        // console.log(this.rolesList, 1)
      })
    },
    handleEdit(index, row) {
      this.EditRolesShow = true
      this.editRoles = row
      // console.log(this.editRoles)
    },
    handleDelete(index, row) {
      this.$axios.delete('roles/' + row.id).then(result => {
        this.$message({
          type: 'success',
          message: result.data.meta.msg
        })
        this.getRolesList()
      })
    },
    addRolesUser() {
      this.$refs.RolesRef.validate(valid => {
        if (valid) {
          this.$axios.post('roles', this.addRoles).then(result => {
            console.log(result.data.data)
            this.$message({
              type: 'success',
              message: result.data.meta.msg
            })
            this.addRolesShow = false
            this.getRolesList()
          })
        } else {
          this.$message({
            type: 'error',
            message: '添加失败'
          })
          return false
        }
      })
    },
    addDialogClosed() {
      this.$refs.RolesRef.resetFields()
    },
    EditRolesUser() {},
    EditDialogClosed() {
      this.$refs.EditRolesRef.resetFields()
    },
    EditRolesUser() {
      // console.log(this.editRoles.id)

      this.$refs.RolesRef.validate(valid => {
        if (valid) {
          this.$axios
            .put('roles/' + this.editRoles.id, {
              roleName: this.editRoles.roleName,
              roleDesc: this.editRoles.roleDesc
            })
            .then(result => {
              this.$message({
                type: 'success',
                message: result.data.meta.msg
              })
              this.EditRolesShow = false
            })
        } else {
          this.$message({
            type: 'error',
            message: '编辑失败'
          })
          return false
        }
      })
    },
    deletePowers(row, id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$axios.delete(`roles/${row.id}/rights/${id}`).then(result => {
            console.log(result.data, 2)
            this.$message({
              type: 'success',
              message: result.data.meta.msg
            })
            row.children = result.data.data
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    setPower(row) {
      this.roleId = row.id
      console.log(this.roleId)
      this.$axios.get('rights/tree').then(result => {
        this.rightsList = result.data.data
      })
      this.getKeys(row, this.defKeys)
      this.setPowerDialog = true
    },
    getKeys(node, arr) {
      if (!node.children) {
        return arr.push(node.id)
      }
      node.children.forEach(item => {
        this.getKeys(item, arr)
      })
    },
    clearDefKeys() {
      this.defKeys = []
    },
    addPower() {
      const key = [
        ...this.$refs.treeRef.getCheckedKeys(),
        ...this.$refs.treeRef.getHalfCheckedKeys()
      ]
      // console.log(key)
      const strKey = key.join(',')
      // console.log(strKey)
      this.$axios
        .post(`roles/${this.roleId}/rights`, { rids: strKey })
        .then(result => {
          console.log(result)
          this.$message({ type: 'success', message: result.data.meta.msg })
          this.setPowerDialog = false
        })
    }
  }
}
</script>
<style scoped>
.el-tag {
  margin: 7px;
}
.dbtop {
  border-top: 1px solid #eee;
}
.dbbottom {
  border-bottom: 1px solid #eee;
}
.vcenter {
  display: flex;
  align-items: center;
}
</style>